package com.saunter.controller;

import com.saunter.model.Job;
import com.saunter.service.JobService;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "管理员编辑职位")
@RestController
@RequestMapping("/admin")
public class JobController {
    @Autowired
    private JobService jobService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("添加职位")
    @PostMapping("/addJob")
    public Map<String, Object> addJob(@RequestParam("department_id")int department_id, @RequestParam("name")String name, @ApiParam(hidden = true) @RequestParam(value = "status",required = false)Integer status) {
        if(1 == jobService.add(new Job(null, department_id, name, status))) {
            return Response.success("添加成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取指定职位信息")
    @GetMapping("/getJob/{job_id}")
    public Map<String, Object> getJob(@PathVariable("job_id") int job_id) {
        return Response.success(jobService.getDepartmentJob(job_id));
    }

    @ApiOperation("修改指定职位名称")
    @PostMapping("/editJob")
    public Map<String, Object> editName(@RequestParam("id")int job_id, @RequestParam("name")String name) {
        if(1 == jobService.editName(new Job(job_id, null, name, null))){
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("删除指定职位")
    @GetMapping("/deleteJob/{id}")
    public Map<String, Object> delete(@PathVariable("id")int job_id) {
        if(1 == jobService.delete(job_id)){
            return Response.success("删除成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }
}
